<template>
  <div class="login_container">
    <div class="login_box">
      <div class="icon_vue">
        <img src="@/assets/logo.png" />
      </div>
      <el-form
        ref="loginForm"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="userLogin">登陆</el-button>
          <el-button type="info" @click="resetValue">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetValue() {
      this.$refs.loginForm.resetFields();
    },
    userLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return;
        let { data: res } = await this.$http.post("/login", this.loginForm);
        console.log(res);
        if (res.meta.status == 200) {
          window.sessionStorage.setItem("token", res.data.token);
          this.$message.success("登陆成功啦！！！");
          this.$router.push("/home");
          return;
        } else {
          this.$message.error("你他妈的登陆失败了哈哈哈");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .icon_vue img {
    z-index: 99;
    position: absolute;
    width: 150px;
    height: 150px;
    top: -75px;
    left: 150px;
    border-radius: 50%;
    background-color: #eee;
    box-shadow: 0 0 10px;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 5px;
    position: relative;
  }
}
.login_form {
  margin: 0 20px;
  margin-top: 115px;

  .login_btn {
    float: right;
  }
}
</style>